<?php

namespace App\V3\V3_2\Controllers;

use App\V3\V3_2\Models\MaterielModel;
use App\V3\V3_2\Services\ProductService;
use App\V3\V3_2\Services\SalesReturnService;
use App\V3\V3_2\Services\SellService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\V3\V3_2\Services\StorageManageService;

class CommonDetailController extends Controller
{
    //销售单详细-without token
    public function sellFind(Request $request)
    {
        //参数验证
        $validator = Validator::make($request->except('role_id'), [
            'id' =>'required|integer',//销售单主键id
        ]);

        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        return response()->json(SellService::detail($request->id));
    }

    //退货单详细-without token
    public function salesReturnFind(Request $request)
    {
        //参数验证
        $validator = Validator::make($request->except('role_id'), [
            'id' =>'required|integer',//销售单主键id
        ],[
            'id.required' => '退货单id缺失'
        ]);

        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        return response()->json(SalesReturnService::find($request->id));
    }

    /**
     * @采购单详细 without token
     * @created 在 2021/12/9 13:52 创建
     * @param Request $request
     * @author: Sun Wu
     */
    public function materielDetail(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->input(), [
            'id' =>'required|integer',//采购单id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $info = MaterielModel::select(
            'a.*',
            'b.nickname',
            'b.username',
        )
            ->with('materielContent', function($query) {
                $query->select(
                    'materiel_id',
                    'unit_price',
                    'num',
                    'a.materiel_name',
                    'a.type_name',
                    'a.materiel_no',
                    'a.materiel_img',
                    //'rest_num',
                    'b.storage_name',
                )
                    ->leftJoin('materiel_type as a', 'a.id', '=', 'materiel_content.type_id')
                    ->leftJoin('storage as b', 'b.id', '=', 'materiel_content.storage_id');
            })
            ->from('materiel as a')
            ->leftJoin('admin as b', 'b.id', '=', 'a.user_id')
            ->where('a.id', $data['id'])
            ->where('a.delete_flag', 0)
            ->orderBy('a.id', 'desc')
            ->get()->toArray();

        return response()->json([
            'code' => 0,
            'msg' => 'success',
            'data' => $info
        ]);
    }

    public function productDetail(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->input(), [
            'id' =>'required|integer',//采购单id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $detail = ProductService::detail($data['id']);

        if(empty($detail)) {
            return response()->json(['code' => 1, 'msg' => '目标单据信息不存在，请刷新后重试', 'data' => '']);
        }

        return response()->json(['code' => 0, 'msg' => 'success', 'data' => $detail]);
    }

    public function readGoodsIdInfo(Request $request){
        //参数验证
        $validator = Validator::make($data=$request->all(), [
            'goods_id' =>'required|integer',//销售单主键id
        ],[
            'goods_id.required' => '商品id缺失'
        ]);

        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        return success(StorageManageService::readGoodsIdInfo($data));
    }
}
